#!/bin/bash


if [ $# -lt 1 ];then
    echo "sh $0 get_kprocess|get_uname|get_uprocess"
    exit 1
fi

arg2=$2
ori_tmpfile=/tmp/ori_$$.process
pro_tmpfile=/tmp/pro_$$.process

pname=$3

function get_uname {
    user_info=( `cat /etc/passwd | grep -v "root" | awk -F ':' '{print $1}' | xargs` )
    user_info_new=()
    j=0
    for ((i=0;i<${#user_info[*]};i++))
    do
	user_ps=`ps -u ${user_info[i]} | grep -v grep | grep -vw 'PID'`
        if [ ! -z "$user_ps" ]; then
            user_info_new[$j]=${user_info[i]}
            j=$(($j+1))
	fi
    done

    printf \
'{
    "data": ['
    for ((i=0;i<${#user_info_new[*]};i++))
    do
        user_ps=`ps -u ${user_info_new[i]} | grep -v grep | grep -vw 'PID'`
        if [ ! -z "$user_ps" ]; then
            if [ $i -lt `expr ${#user_info_new[*]} - 1` ]; then
                echo '{"{#UNAME}": "'${user_info_new[i]}'"},'
            else
                echo '{"{#UNAME}": "'${user_info_new[i]}'"}'
            fi
        fi
    done
    echo \
']}'

}

function get_uprocess {
    if [ ! -z ${arg2} ]; then
        #uid,pid,ppid,tid,pcpu,pmem：用户ID，进程IP，父进程ID，线程ID(lwp)，CPU(%)，内存(%)
        #stat,rss,vsz,tty：进程状态，常驻集大小(KB)，虚拟内存大小(KB)，终端类型
        #pri,ni: 进程优先级(小优)，进程NICE值[PRI(new)=PRI(old)+nice(-20-19)]
        #stime,time：进程启动时间，该进程实际使用CPU的时间(单位是小时：分钟：秒)
        ps -u ${arg2} -o user,pid,ppid,tid,pcpu,pmem,stat,rss,vsz,tty,pri,ni,stime,time,cmd | grep -Ev 'grep|kworker|defunct|USER' > ${ori_tmpfile}
        while read line
        do
            user=`echo $line | awk '{print $1}'`
            pid=`echo $line | awk '{print $2}'`
            ppid=`echo $line | awk '{print $3}'`
            tid=`echo $line | awk '{print $4}'`
            pcpu=`echo $line | awk '{print $5}'`
            pmem=`echo $line | awk '{print $6}'`
            stat=`echo $line | awk '{print $7}'`
            rss=`echo $line | awk '{print $8}'`
            vsz=`echo $line | awk '{print $9}'`
            tty=`echo $line | awk '{print $10}'`
            pri=`echo $line | awk '{print $11}'`
            ni=`echo $line | awk '{print $12}'`
            stime=`echo $line | awk '{print $13}'`
            ttime=`echo $line | awk '{print $14}'`
            cmd="`echo $line | awk '{for(i=15;i<=NF;i=i+1)printf " "$i;print ""}'`"
            echo "{\"user\":\"${user}\",\"pid\":\"${pid}\",\"ppid\":\"${ppid}\",\"tid\":\"${tid}\",\"pcpu\":\"${pcpu}\",\"pmem\":\"${pmem}\",\"stat\":\"${stat}\",\"rss\":\"${rss}\",\"vsz\":\"${vsz}\",\"tty\":\"${tty}\",\"pri\":\"${pri}\",\"ni\":\"${ni}\",\"stime\":\"${stime}\",\"time\":\"${ttime}\",\"cmd\":\"${cmd}\"}" >> ${pro_tmpfile}
        done < ${ori_tmpfile}
    
        echo '['
            if [[ -f ${pro_tmpfile} && -s ${pro_tmpfile} ]];then
                num_of_line=`wc -l ${pro_tmpfile} |awk '{print $1}'`
                IFS=$'\n' # 定义分割符
                i=1
                for line in `cat ${pro_tmpfile}`
                do
                    if [ $i -eq $num_of_line ]; then
                        echo "$line"
                    else
                        echo "$line,"
                    fi
                    let i+=1
                done
        echo ']'
            fi

    fi
        rm -rf $ori_tmpfile $pro_tmpfile
}

function get_pname {
    if [ ! -z "${pname}" ]; then
        p_num=`ps aux | grep -v grep|grep -e "${pname}"[/.]| wc -l`
        printf \
'{
    "data": ['

        ps aux | grep -v grep|grep -e "${pname}"[/.] > /tmp/shsnc_get_pname.txt

        i=0
        while read line
        do
            pname_pid=$(echo ${pname}_`echo ${line} | awk '{print $2}'`)
            if [ $i -eq `expr ${p_num} - 1` ];then
                echo "{\"{#PP}\":\"$pname_pid\"}"
            else
                echo "{\"{#PP}\":\"$pname_pid\"},"
            fi
            i=$(($i+1))
        done < /tmp/shsnc_get_pname.txt
        rm -rf /tmp/shsnc_get_pname.txt
    fi
    echo \
']}'
}

function get_pname_pid {
    ps aux | grep -v grep|grep -e "${pname}"[/.] > /tmp/shsnc_get_pname_pid.txt
    while read line
    do
        user=`echo ${line} | awk '{print $1}'`
        pid=`echo ${line} | awk '{print $2}'`
        pcpu=`echo ${line} | awk '{print $3}'`
        pmem=`echo ${line} | awk '{print $4}'`
        vsz=`echo ${line} | awk '{print $5}'`
        rss=`echo ${line} | awk '{print $6}'`
        tty=`echo ${line} | awk '{print $7}'`
        stat=`echo ${line} | awk '{print $8}'`
        start=`echo ${line} | awk '{print $9}'`
        ttime=`echo ${line} | awk '{print $10}'`
        cmd=`echo ${line} | awk '{for(i=11;i<=NF;i=i+1)printf " "$i;print ""}'`

        pname_pid=$(echo ${pname}_`echo ${line} | awk '{print $2}'`)
        if [ "$arg2" == "$pname_pid" ];then
            echo "{\"pname_pid\":\"${pname_pid}\",\"user\":\"${user}\",\"pid\":\"${pid}\",\"pcpu\":\"${pcpu}\",\"pmem\":\"${pmem}\",\"stat\":\"${stat}\",\"rss\":\"${rss}\",\"vsz\":\"${vsz}\",\"tty\":\"${tty}\",\"start\":\"${start}\",\"time\":\"${ttime}\",\"cmd\":\"${cmd}\"}"
        fi

    done  < /tmp/shsnc_get_pname_pid.txt
    rm -rf /tmp/shsnc_get_pname_pid.txt
}

function get_kprocess {
    #系统工作进程数量
    kworker_num=`ps -ef | grep -v grep |grep kworker | wc -l`
    #僵尸进程数量
    defunct_num=`ps -ef | grep -v grep |grep defunct | wc -l`
    echo '{"kworker_num": "'${kworker_num}'","defunct_num": "'${defunct_num}'"}'
}

$1